const db = require('../../db');



//创建新退款记录
exports.addNewRefund = (order_id,reason,amount,callback) => {
    const q = "INSERT INTO `module2_refund`(order_id,refund_reason,amount) values(?,?,?);";
    const values =[order_id,reason,amount];

    db.query(q, values, (err, results) => {
        if (err) {
          callback(err);
        } else if (results.affectedRows !== 1) {
          callback(new Error("更新失败，请稍后再试！"));
        } else {
          callback(null, "更新成功！", 0);
        }
    });
}

//输入拒绝退款理由，订单号，更新拒绝退款理由
exports.updateRefundRefuseReason = (orderId,response,flag, callback) => {
  const q = "UPDATE module2_refund SET refund_response = ? , if_admitted =? WHERE order_id = ?;";

  const values = [response, flag,orderId];

  db.query(q, values, (err, results) => {
      if (err) {
        callback(err);
      } else if (results.affectedRows !== 1) {
        callback(new Error("更新失败，请稍后再试！"));
      } else {
        callback(null, "更新成功！", 0);
      }
  });
};

//输入订单号，查询退款信息
exports.RefundIdSearch = (req, res) => {
    const q = "Select * from module2_refund where order_id = ? ;";
    const values = req.params.order_id;
    db.query(q, values,(err, results) => {
      if (err) return(err.message)
        res.send({
          status: 0,
          message: '数据返回成功！',
          data: results,
        })
    });
};